var express = require('express');
var router = express.Router();
let mongoose = require('mongoose');

// 查找已经做过的试卷
router.post('/query', async function (req, resp) {
    // find搜索对应id
    let { studentId } = req.body;

    //tested就是该同学已完成的试卷
    let tested = await mongoose.model('testedsModel').find({
        studentId: studentId
    }).populate({
        path: 'testId',
        populate: {
            path: 'exerciseId'
        }
    })
    let testedIdArr = [];
    for (let i = 0; i < tested.length; i++) {
        testedIdArr.push(tested[i].testId.id);
    }
    // console.log(testedIdArr);



    //筛选未做的试卷 test就是未做的试卷
        let test=await mongoose.model('testsModel').find({
            _id: {
                $nin:[...testedIdArr]
          }
        })
    // }
    // console.log(test);



    // 输出试卷信息

    if (tested) {
        resp.send({
            code: 1,
            massage: '获取试卷信息成功！',
            data: { tested, test }
        });

    } else {
        resp.send({
            code: 2,
            massage: '获取试卷信息失败！',
        });
    }
})



module.exports = router;
